package com.unieagle.exam.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.unieagle.exam.R;
import com.unieagle.exam.model.Exam;
import com.unieagle.exam.model.Question;

public class ErrorExamResultActivity extends BaseActivity{

	private Exam mExam;
	
	private TextView mTvRight, mTvWrong, mTvReview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.error_exam_result_activity_layout);
		init();
	}
	
	protected void init() {
		super.init();
		mTvTitle.setText(R.string.error_exam_result);
		mTvRight = (TextView)findViewById(R.id.right_cnt);
		mTvWrong = (TextView) findViewById(R.id.wrong_cnt);
		mTvReview = (TextView) findViewById(R.id.review);
		mExam = (Exam)getIntent().getParcelableExtra("exam");
		mBBBack.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		if(mExam != null) {
			initResult();
		}
	}
	
	private void initResult() {
		mTvRight.setText(String.valueOf(mExam.calCorrectCnt2()));
		mTvWrong.setText(String.valueOf(mExam.getTotal_cnt() - mExam.calCorrectCnt2()));
		if(mExam.getTotal_cnt() - mExam.calCorrectCnt2() == 0) {
			mTvReview.setText(R.string.reexam);
		} else {
			mTvReview.setText(R.string.error_exam_detail);
		}
		mTvReview.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent i;
				if(mExam.getTotal_cnt() - mExam.calCorrectCnt2() == 0) {
					i = new Intent(ErrorExamResultActivity.this, SubjectChooseActivity.class);
					i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(i);
				} else {
					Exam errorExma = new Exam();
					for(Question q : mExam.getQuestions()) {
						if(!q.isBlank() && !q.isCorrect()) {
							Question newq = new Question();
							newq.setId(q.getId());
							newq.setSubjectId(q.getSubjectId());
							newq.setContent(q.getContent());
							newq.setType(q.getType());
							newq.setTypeName(q.getTypeName());
							newq.setRequired(q.getRequired());
							newq.setAnswer(q.getAnswer());
							errorExma.getQuestions().add(newq);
						}
					}
					i = new Intent(ErrorExamResultActivity.this, ExamResultDetailActivity.class);
					i.putExtra("from_error_result", true);
					Bundle bundle = new Bundle();
					bundle.putParcelable("exam", errorExma);
					i.putExtras(bundle);
					startActivity(i);
				}
			}
		});
	}
	
	@Override
	public void finish() {
		startActivity(new Intent(ErrorExamResultActivity.this, PreExamActivity.class));
	}
	
}
